<?php
class FeedsController extends AppController {

	var $name = 'Feeds';
	var $components = array('FeedReader');
	var $helpers = array('Html', 'Form', 'Javascript', 'Text', 'Session');

	function index() {
		$this->set('feeds', $this->paginate());
	}

	function scan($id = null) {
		$this->FeedReader->scan($id);
	}

	function view($id = null) {
		$feed = $this->Feed->find('first',array(
			'conditions'=>array('id'=>$id),
			'contain'=>(array('Favorite'))
		));
		$this->set('feed', $feed);
	}

	function checkXPath() {
		/*
		 * Use this to check if the download XPath expression is correctly evaluated
		 */
		$this->layout = 'ajax';
		Configure::write('debug',0);
		
		if(isset($this->params['url']['page'])) {
			$page = $this->params['url']['page'];
		} else {
			$xml = $this->FeedReader->loadFeed($this->params['url']['feedurl']);
			$page = $xml->channel->item[0]->link;
		}
		$urlpattern = $this->params['url']['urlpattern'];
		$xpath = $this->params['url']['xpath'];
		if($page && $xpath) {
			$xml = $this->FeedReader->cleanupPage(file_get_contents($page));
			list($_) = $xml->xpath(str_replace('/tbody','',$xpath));
			$_ = $_->attributes();
			$downloadurl = $_['href'];
		}
		$this->set(compact(array('downloadurl','xpath','page','urlpattern')));
	}

	function findXPath() {
		/*
		 * Use this to find the download XPath expression
		 */
		$this->layout = 'ajax';
//		Configure::write('debug',0);
		
		if(isset($this->params['url']['page'])) {
			$page = $this->params['url']['page'];
		} else {
			$xml = $this->FeedReader->loadFeed($this->params['url']['feedurl']);
			$page = $xml->channel->item[0]->link;
		}
		$text = file_get_contents($page);
		$this->set('page',preg_replace('/<head.*\/head>|<script.*?\/script>/is','',$text));
	}


	function checkRegex() {
		/*
		 * Use this to check if the download regex is correctly evaluated
		 */
		$this->layout = 'ajax';
		Configure::write('debug',0);
	
		if(isset($this->params['form']['page'])) {
			$page = $this->params['form']['page'];
		} else {
			$xml = $this->FeedReader->loadFeed($this->params['form']['feedurl']);
			$page = $xml->channel->item[0]->link;
		}
		$urlpattern = urldecode($this->params['form']['urlpattern']);
		$regex = str_replace('%2B','+',urldecode($this->params['form']['regex']));
		if($page && $regex) {
			$html = file_get_contents($page);
			preg_match("/".str_replace('/','\/',$regex)."/im",$html,$_);
			if(count($_)) {
				$downloadurl = $_[1];
			}
			
		}
		$this->set(compact(array('downloadurl','regex','page','urlpattern')));
		
	}

	function checkReplace() {
		/*
		 * Use this to check if the download regex is correctly evaluated
		 */
		$this->layout = 'ajax';
		Configure::write('debug',0);
		$xml = $this->FeedReader->loadFeed($this->params['form']['feedurl']);
//		$page = $xml->channel->item[0]->link;
		if($this->params['form']['search'] && $this->params['form']['replace'] && $page) {
			$downloadurl = preg_replace($this->params['form']['search'], $this->params['form']['replace'],$page);
			$this->set('downloadurl');	
		}	else {
			$this->set('message',"Unable to retrieve feed. Try again later.");
		}
		
	}


	function add() {
		$favorites = $this->Feed->Favorite->find('list');
		$categories = $this->Feed->Category->find('list');
		$this->set(compact('favorites','categories'));
		$this->render('edit');
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Feed->create();
		}
		if (!empty($this->data)) {
			if ($this->Feed->save($this->data)) {
				$this->flash(__('The Feed has been saved.', true), array('action'=>'edit'));
			} else {
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Feed->read(null, $id);
		}
		$favorites = $this->Feed->Favorite->find('list');
		$categories = $this->Feed->Category->find('list');
		$this->set(compact('favorites','categories'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Feed', true), array('action'=>'index'));
		}
		if ($this->Feed->del($id)) {
			$this->flash(__('Feed deleted', true), array('action'=>'index'));
		}
	}

	function preview() {
		/*
		 * @param none
		 * Requires $feedurl in url query
		 */
		$this->layout='ajax';
		Configure::write('debug',0);
		$xml  = $this->FeedReader->loadFeed($this->params['url']['feedurl']);
//		$this->out($xml);
		$this->set('xml', $xml);
		$this->set('maxitems',4);
	}

	function show($id = null) {
		/*
		 * @param none
		 * Requires $feedurl in url query
		 */
		$this->layout='ajax';
		list($id,$furl) = explode('__',$this->params['url']['feed']);
//		Configure::write('debug',0);
		$xml  = $this->FeedReader->loadFeed($furl);
//		$this->out($xml);
		$this->set('xml', $xml);
		$this->set('maxitems',15);
	}
	
	function out($msg) {
		$this->Session->setFlash($msg);
	}
}
?>